﻿#include "historyrecord.h"
#include "utils.h"

#include <QFile>
#include <QDebug>
#include <QTime>
#include <QCoreApplication>
#include <QDir>
#include <QStandardPaths>

Utils::Utils()
{

}

// 向文件中写入历史记录
void Utils::writeHistory(QString url, QString filePath)
{
    if(filePath.trimmed().length() == 0)filePath = "history.txt";
    // 数据文件路径
//    qDebug()<<QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + filePath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
        qDebug() << "write file open file error";
        return;
    }
    HistoryRecord historyRecord(url);
    QString data = historyRecord.historyString() + "\n";
    QTextStream out(&file);
    out << data;
    file.close();
}


// 向文件中写入历史记录，包括收藏夹
void Utils::writeBookMark(QString url, QString filePath)
{
    qDebug()<<"writeBookMark";
    if(filePath.trimmed().length() == 0)filePath = "bookmark.txt";
    // 数据文件路径
//    qDebug()<<QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + filePath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)){
        qDebug() << "write file open file error";
        return;
    }
    HistoryRecord historyRecord(url);
    QString data = historyRecord.historyString() + "\n";
    QTextStream out(&file);
    out << data;
    file.close();
}

QVector<QString> Utils::loadHistory(QString filePath){
    if(filePath.trimmed().length() == 0)filePath = "history.txt";
    QVector<QString> res;
    QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "open file error";
        return res;
    }
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        res.push_back(line);
    }
    file.close();
    return res;
}

QVector<HistoryRecord> Utils::loadHistoryRecord(QString filePath)
{
    if(filePath.trimmed().length() == 0)filePath = "history.txt";
    QVector<HistoryRecord> res;
    QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "open file error";
        return res;
    }
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        HistoryRecord rec;
        rec.load(line);
        res.push_back(rec);
    }
    file.close();
    return res;
}

QVector<HistoryRecord> Utils::loadBookMarkRecord(QString filePath)
{
    if(filePath.trimmed().length() == 0)filePath = "bookmark.txt";
    QVector<HistoryRecord> res;
    QFile file(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "open file error";
        return res;
    }
    QTextStream in(&file);
    while (!in.atEnd()) {
        QString line = in.readLine();
        HistoryRecord rec;
        rec.load(line);
        res.push_back(rec);
    }
    file.close();
    return res;
}

void Utils::deletePathFile(QString files)
{
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + files;
    qDebug()<<"delete path = "<<filePath;
    if(QDir().exists(filePath)){
        QFileInfo fileInfo(filePath);
        if(fileInfo.isFile()){
            bool res = QFile::remove(filePath);
            if(!res){
                qDebug() << filePath + ", delete defeat";
            }
        }else if(fileInfo.isDir()){
            QDir qDir(filePath);
            qDir.removeRecursively();
        }
    }
}

// 删除历史记录
bool Utils::deleteHistroy(QString file)
{
    if(file.trimmed().length() == 0)file = "history.txt";
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + file;
    if(QDir().exists(filePath)){
        QFileInfo fileInfo(filePath);
        if(fileInfo.isFile()){
            QFile::remove(filePath);
        }else if(fileInfo.isDir()){
            QDir qDir(filePath);
            qDir.removeRecursively();
        }
    }
    return true;
}

bool Utils::deleteBookMark(QString file)
{
    if(file.trimmed().length() == 0)file = "bookmark.txt";
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/" + file;
    if(QDir().exists(filePath)){
        QFileInfo fileInfo(filePath);
        if(fileInfo.isFile()){
            QFile::remove(filePath);
        }else if(fileInfo.isDir()){
            QDir qDir(filePath);
            qDir.removeRecursively();
        }
    }
    return true;
}


